﻿using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MRO.ClientApplication.Util
{
    public static class CommonExtension
    {
        private static ILog logger = LogManager.GetLogger(typeof(CommonExtension));
        public static string LogInfo(this string str, string content)
        {
            logger.Info(content + str);
            return str;
        }

        public static string LogDebug(this string str, string content)
        {
            logger.Debug(content + str);
            return str;
        }

        public static string LogError(this string str, string content)
        {
            logger.Error(content + str);
            return str;
        }

        public static string GetNPOIDateTime(string dateStr)
        {
            // NPOI导入日期格式处理
            string mytime = dateStr.Trim(); // dateStr为Excel导入的日期值
            IFormatProvider culture = new CultureInfo("zh-CN", true);
            string[] expectedFormats = { "dd-M月-yyyy" };
            DateTime dateTime;
            if (DateTime.TryParseExact(mytime, expectedFormats, culture, DateTimeStyles.AllowInnerWhite, out dateTime))
            {
                mytime = dateTime.ToString("yyyy-MM-dd");
            }
            else
            {
                mytime = dateStr.Trim();
            }
            return mytime;
        }

        public static string ToJsonLower(this object source)
        {
            if (source == null)
            {
                return string.Empty;
            }
            var serializerSettings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };
            return JsonConvert.SerializeObject(source, Formatting.None, serializerSettings);
        }

        public static string GetNumberString(this string source)
        {
            string result = System.Text.RegularExpressions.Regex.Replace(source, @"[^0-9]+", "");
            return result;
        }
        //string result = System.Text.RegularExpressions.Regex.Replace(str, @"[^0-9]+", "");

        public static string AllTrim(this string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return null;
            }
            else
            {
                return source.Trim();
            }
        }

        public static Dictionary<string, object> ObjectToDictionary(object obj)
        {
            var dic = new Dictionary<string, object>();
            foreach (var prop in obj.GetType().GetProperties())
            {
                dic.Add(prop.Name, prop.GetValue(obj, null));
            }
            return dic;
        }

        public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
        {
            return condition ? query.Where(predicate) : query;
        }

        public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, int, bool>> predicate)
        {
            return condition ? query.Where(predicate) : query;
        }

        public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> query, bool condition, Func<T, bool> predicate)
        {
            return condition ? query.Where(predicate) : query;
        }




    }
}
